<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCommunityInteractionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('community_interactions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('share_id');
            $table->enum('interaction_type', ['like', 'dislike', 'comment']);
            $table->text('comment_content')->nullable()->comment('仅当interaction_type为comment时使用');
            $table->boolean('is_hidden')->default(false)->comment('是否被隐藏');
            $table->timestamp('created_at')->useCurrent();
            
            // 外键约束
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('share_id')->references('id')->on('community_shares')->onDelete('cascade');
            
            // 唯一约束（用户对同一分享的同类型互动只能有一次，评论除外）
            $table->unique(['user_id', 'share_id', 'interaction_type'], 'unique_user_share_interaction');
            
            // 索引
            $table->index(['share_id']);
            $table->index(['user_id']);
            $table->index(['interaction_type']);
            $table->index(['is_hidden']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('community_interactions');
    }
}